﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WHM3.ManagerPage
{
    public partial class ManagerPage : System.Web.UI.Page
    {
        LinkedList<Shift> shifts;
        LoginDetails details;
        protected void Page_Load(object sender, EventArgs e)
        {
            details = (LoginDetails)Session["managerDetails"];
            try
            {
                if (details == null || !details.isManager)
                {
                    //hack attempt
                    Response.Redirect("~/General/unAuthorized.aspx");
                }
            }
            catch
            {
                //hack attempt
                Response.Redirect("~/General/unAuthorized.aspx");
            }

            //post feedback
            this.changePasswordDiv.Visible = false;
            if (Session["errMsg"] != null)
            {
                this.eMsgText.Text = (string)Session["errMsg"];
                this.eMsgText.Visible = true;
                Session["errMsg"] = null;
            }
            else
            {
                if (details.checkedIntoWork)
                {
                    string lastOpen=null;
                    try
                    {
                        lastOpen = BL.getLastOpenShift(details.ID);
                    }
                    catch
                    {
                        this.HelloArgs.Text = "There is an error somewhere...";
                    }
                    this.eMsgText.Text = "You have been at work for: " +
                        (DateTime.Now.Hour - (int.Parse(lastOpen.Split(new string[] { "::" }, StringSplitOptions.None)[1].Split(':')[0]))).ToString()
                        + " Hours";
                    this.eMsgText.Visible = true;
                }
            }

            if (details.checkedIntoWork)
            {
                this.Button1.Visible = false;
                this.Button2.Visible = true;
            }
            else
            {
                this.Button1.Visible = true;
                this.Button2.Visible = false;
            }

            BL.init();
            try
            {

                Employee employee = BL.getEmployeeByLoginDetails(details);
                this.nameLabel.Text = details.ID + " " + employee.lastName + " " + employee.firstName;
                this.PositionLabel.Text = employee.Position;
                shifts = BL.getAllShiftsById(employee.id);
                this.HelloArgs.Text = "Hello";
            }
            catch
            {
                this.HelloArgs.Text = "There is an error somewhere...";
            }

            initLogoutButton();

            showShifts();
        }

        protected void makeCoffe(object sender, EventArgs e)
        {
            Response.Redirect("~/General/MakeCoffee.aspx");
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            try
            {
                shifts = BL.getAllShiftsById(((LoginDetails)Session["managerDetails"]).ID);
            }
            catch
            {
                Session["errMsg"] = "something went trerribly wrong...";
                Response.Redirect("~/Account/Login.aspx");
            }
            foreach (Shift shift in shifts)
            {
                if (e.Day.Date.Month.ToString() == shift.getStartMonth() && e.Day.DayNumberText == shift.getStartDay() && !e.Day.IsOtherMonth)
                {
                    e.Cell.BackColor = System.Drawing.Color.Red;
                }

            }
        }

        protected void Selection_Change(object sender, EventArgs e)
        {
            SelectedDatesCollection theDates = Calendar1.SelectedDates;

            DateTime date = theDates[0];

            string dateToShow = date.Day.ToString() + "/" + date.Month.ToString() + "/" + date.Year.ToString();
            Session["showShifts"] = dateToShow;

            Response.Redirect("~/ManagerPage/ManagerPage.aspx");
        }

        private void showShifts()
        {
            if (Session["showShifts"] != null && !IsPostBack)
            {
                this.editShiftDiv.Visible = true;

                int counter = 0;
                try
                {
                    shifts = BL.getAllShiftsByIdAndDate(details.ID, (string)Session["showShifts"]);
                }
                catch
                {
                    Session["errMsg"] = "We are experiancing technical difficulties, please stay tuned for our feature programming";
                    Response.Redirect("~/ManagerPage/ManagerPage.aspx");
                }
                Control mainDivider = this.myInfo;
                Control secondDiv = mainDivider.FindControl("editShiftDiv");

                if (shifts == null)
                {
                    editShiftDiv.Visible = false;
                    testLabel.Visible = true;
                }
                else if(shifts.Count == 0)
                {
                    editShiftDiv.Visible = false;
                    testLabel.Visible = true;
                }
                foreach (Shift shift in shifts)
                {
                    counter++;
                    string startTextBoxName = "editShiftTextBox" + counter;
                    string startLabenamel = "editShiftLabel" + counter;
                    string fromInfoLabelName = "infoLabel" + counter;
                    counter++;
                    string endTextBoxName = "editShiftTextBox" + counter;
                    string endLabelName = "editShiftLabel" + counter;
                    string toInfoLabelName = "infoLabel" + counter;

                    TextBox startTexbox = (TextBox)(secondDiv.FindControl(startTextBoxName));
                    TextBox endTexbox = (TextBox)(secondDiv.FindControl(endTextBoxName));

                    Label startLabel = (Label)secondDiv.FindControl(startLabenamel);
                    Label endLabel = (Label)secondDiv.FindControl(endLabelName);

                    Label fromInfoLabel = (Label)secondDiv.FindControl(fromInfoLabelName);
                    Label toInfoLabel = (Label)secondDiv.FindControl(toInfoLabelName);

                    startTexbox.Visible = true;
                    startTexbox.Text = shift.getStartHour() + ":" + shift.getStartMin();
                    startLabel.Text = shift.getStartHour() + ":" + shift.getStartMin();
                    fromInfoLabel.Visible = true;

                    endTexbox.Visible = true;
                    endTexbox.Text = shift.getEndHour() + ":" + shift.getEndMin();
                    endLabel.Text = shift.getEndHour() + ":" + shift.getEndMin();
                    toInfoLabel.Visible = true;

                    //TextBox startTexbox = new TextBox();
                    //TextBox endTexbox = new TextBox();

                    //startTexbox.Text = shift.getStartHour() + ":" + shift.getStartMin();
                    //startTexbox.ID = shift.getStartHour() + ":" + shift.getStartMin();
                    //startTexbox.Attributes.Add("Width","50px");

                    //endTexbox.Text = shift.getEndHour()+":"+shift.getEndMin();
                    //endTexbox.ID = shift.getEndHour() + ":" + shift.getEndMin();
                    //endTexbox.Attributes.Add("Width", "50px");

                    //editShiftDiv.Controls.Add(startTexbox);
                    //editShiftDiv.Controls.Add(endTexbox);

                }
                while (counter < 20)
                {
                    counter++;
                    string startTextBoxName = "editShiftTextBox" + counter;
                    string startLabenamel = "editShiftLabel" + counter;
                    string fromInfoLabelName = "infoLabel" + counter;
                    counter++;
                    string endTextBoxName = "editShiftTextBox" + counter;
                    string endLabelName = "editShiftLabel" + counter;
                    string toInfoLabelName = "infoLabel" + counter;

                    TextBox startTexbox = (TextBox)(secondDiv.FindControl(startTextBoxName));
                    TextBox endTexbox = (TextBox)(secondDiv.FindControl(endTextBoxName));

                    Label startLabel = (Label)secondDiv.FindControl(startLabenamel);
                    Label endLabel = (Label)secondDiv.FindControl(endLabelName);

                    Label fromInfoLabel = (Label)secondDiv.FindControl(fromInfoLabelName);
                    Label toInfoLabel = (Label)secondDiv.FindControl(toInfoLabelName);

                    startTexbox.Visible = false;
                    startLabel.Visible = false;
                    fromInfoLabel.Visible = false;

                    endTexbox.Visible = false;
                    endLabel.Visible = false;
                    toInfoLabel.Visible = false;
                }

            }
            else
            {
                int counter = 0;

                Control mainDivider = this.myInfo;
                Control secondDiv = mainDivider.FindControl("editShiftDiv");

                this.editShiftDiv.Visible = false;
                while (counter < 20)
                {
                    counter++;
                    string startTextBoxName = "editShiftTextBox" + counter;
                    string startLabenamel = "editShiftLabel" + counter;
                    string fromInfoLabelName = "infoLabel" + counter;
                    counter++;
                    string endTextBoxName = "editShiftTextBox" + counter;
                    string endLabelName = "editShiftLabel" + counter;
                    string toInfoLabelName = "infoLabel" + counter;

                    TextBox startTexbox = (TextBox)(secondDiv.FindControl(startTextBoxName));
                    TextBox endTexbox = (TextBox)(secondDiv.FindControl(endTextBoxName));

                    Label startLabel = (Label)secondDiv.FindControl(startLabenamel);
                    Label endLabel = (Label)secondDiv.FindControl(endLabelName);

                    Label fromInfoLabel = (Label)secondDiv.FindControl(fromInfoLabelName);
                    Label toInfoLabel = (Label)secondDiv.FindControl(toInfoLabelName);

                    startTexbox.Visible = false;
                    startLabel.Visible = false;
                    fromInfoLabel.Visible = false;

                    endTexbox.Visible = false;
                    endLabel.Visible = false;
                    toInfoLabel.Visible = false;

                }

            }
        }

        protected void saveChanges(object sender, System.EventArgs e)
        {
            int counter = 0;
            
            Control mainDivider = this.myInfo;
            Control secondDiv = mainDivider.FindControl("editShiftDiv");

            while (counter < 20)
            {
                counter++;
                string startTextBoxName = "editShiftTextBox" + counter;
                string startLabenamel = "editShiftLabel" + counter;
                counter++;
                string endTextBoxName = "editShiftTextBox" + counter;
                string endLabelName = "editShiftLabel" + counter;

                TextBox startTexbox = (TextBox)(secondDiv.FindControl(startTextBoxName));
                TextBox endTexbox = (TextBox)(secondDiv.FindControl(endTextBoxName));

                Label startLabel = (Label)secondDiv.FindControl(startLabenamel);
                Label endLabel = (Label)secondDiv.FindControl(endLabelName);

                if (endTexbox.Text != endLabel.Text || startTexbox.Text != startLabel.Text)
                {

                    Shift befor = new Shift(details.ID, Session["showShifts"] + "::" + startLabel.Text, Session["showShifts"] + "::" + endLabel.Text);
                    Shift after = new Shift(details.ID, Session["showShifts"] + "::" + startTexbox.Text, Session["showShifts"] + "::" + endTexbox.Text);
                    try
                    {
                        BL.updateShift(befor, after);
                    }
                    catch (ArgumentNullException)
                    {
                        shiftErrorLabel.Text = "connection error";
                        return;
                    }
                    startLabel.Text = startTexbox.Text;
                    endLabel.Text = endTexbox.Text;
                }
            }
            //foreach (Shift shift in shifts)
            //{
            //    Control mainDivider = this.myInfo;
            //    Control secondDiv = mainDivider.FindControl("editShiftDiv");

            //    string startTextBoxName = "MainContent_"+shift.getStartHour() + ":" + shift.getStartMin();
            //    string endTextBoxName = shift.getEndHour() + ":" + shift.getEndMin();

            //    TextBox startTexbox = (TextBox)(secondDiv.FindControl("09:00"));
            //    TextBox endTexbox = (TextBox)(secondDiv.FindControl(endTextBoxName));

            //    if (startTexbox.ID != startTexbox.Text || endTexbox.ID != endTexbox.Text)
            //    {
            //        Shift toUpdate = new Shift(shift.id, startTexbox.Text, endTexbox.Text);
            //        try
            //        {
            //            BL.updateShift(shift, toUpdate);
            //        }
            //        catch (ArgumentNullException)
            //        {
            //            shiftErrorLabel.Text = "connection error";
            //            return;
            //        }

            //        startTexbox.ID = startTexbox.Text;
            //        endTexbox.ID = endTexbox.Text;
            //    }
            //}
            shiftErrorLabel.Text = "updated";
        }

        private void initLogoutButton()
        {
            //experimental logout feature
            Control h = this.Header.FindControl("LoginView1");
            Control h2 = h.FindControl("loginLink");
            ((System.Web.UI.HtmlControls.HtmlAnchor)h2).HRef = "./Account/Login.aspx";
            ((System.Web.UI.HtmlControls.HtmlAnchor)h2).InnerText = "Log Out";
        }

        protected void ManageEmployees(object sender, EventArgs e)
        {
            Session["manager"] = true;
            Session["myobject"] = this.details;
            Response.Redirect("~/ManagerPage/ManageEmployees.aspx");
        }

        protected void ReportsSystem(object sender, EventArgs e)
        {
            Session["manager"] = true;
            Session["myobject"] = this.details;
            Response.Redirect("~/ManagerPage/Reports.aspx");
        }

        protected void checkIntoWork(object sender, EventArgs e)
        {
            try
            {
                if (!details.checkedIntoWork)
                {
                    if (BL.checkIntoWork(details.ID))
                    {
                        Session["errMsg"] = "your shift began at: "
                            + DateTime.Now.Day.ToString() + "/"
                            + DateTime.Now.Month.ToString()
                            + "/" + DateTime.Now.Year.ToString()
                            + "::" + DateTime.Now.Hour + ":"
                            + DateTime.Now.Minute.ToString();
                        details.checkedIntoWork = true;
                        Response.Redirect("~/ManagerPage/ManagerPage.aspx");
                    }
                    else
                    {
                        Session["errMsg"] = "We are experiancing technical difficulties, please stay tuned for our feature programming";
                        Response.Redirect("~/ManagerPage/ManagerPage.aspx");
                    }
                }
            }
            catch
            {
                Session["errMsg"] = "We are experiancing technical difficulties, please stay tuned for our feature programming";
                Response.Redirect("~/ManagerPage/ManagerPage.aspx");
            }
        }

        protected void checkOutOfWork(object sender, EventArgs e)
        {
            try
            {
                if (details.checkedIntoWork)
                {
                    if (BL.checkOutOfWork(details.ID))
                    {
                        Session["errMsg"] = "Your shift ended at: "
                            + DateTime.Now.Day.ToString() + "/"
                            + DateTime.Now.Month.ToString()
                            + "/" + DateTime.Now.Year.ToString()
                            + "::" + DateTime.Now.Hour + ":"
                            + DateTime.Now.Minute.ToString();
                        details.checkedIntoWork = false;
                        Response.Redirect("~/ManagerPage/ManagerPage.aspx"); ;
                    }
                }
            }
            catch
            {
                Session["errMsg"] = "We are experiancing technical difficulties, please stay tuned for our feature programming";
                Response.Redirect("~/ManagerPage/ManagerPage.aspx");
            }
        }

        protected void changePasswordDivOpener(object sender, EventArgs e)
        {
            this.changePasswordDiv.Visible = true;
        }

        protected void changePassword(object sender, EventArgs e)
        {
            try
            {
                if (BL.changePassword(BL.getEmployeeByLoginDetails((LoginDetails)Session["myobject"]), this.newPassTextBox.Text))
                {
                    Session["errMsg"] = "Password Change succesful";
                    Response.Redirect("~/ManagerPage/ManagerPage.aspx");
                }
            }
            catch
            {
                Session["errMsg"] = "there is an error with the DB";
                Response.Redirect("~/ManagerPage/ManagerPage.aspx");
            }
        }

        protected void addNewShiftClick(object sender, EventArgs e)
        {
            shiftalendar.Visible = true;
            changePasswordDiv.Visible = false;
            myInfo.Visible = false;
            editShiftDiv.Visible = true;
        }

        protected void Selection_Change2(object sender, EventArgs e)
        {
            addNewShift.Visible = true;
        }
        protected void addNewShiftFunction(object sender, EventArgs e)
        {
            SelectedDatesCollection theDates = Calendar2.SelectedDates;

            DateTime date = theDates[0];

            string dateToShow = date.Day.ToString() + "/" + date.Month.ToString() + "/" + date.Year.ToString();

            try
            {
                if (BL.addNewShift(details.ID, dateToShow + "::" + addNewShiftFrom.Text, dateToShow + "::" + addNewShiftTo.Text))
                {
                    addNewShiftMSG.Text = "added";
                }
                else
                {
                    addNewShiftMSG.Text = "not added, check format";
                }
            }
            catch
            {
                addNewShiftMSG.Text = "Error";
            }
        }
    }
}